﻿package net.jmp0.toast.entities.bases
{
	import flash.geom.ColorTransform;
	import flash.geom.Rectangle;
	import net.jmp0.toast.utils.GUID;
	import net.jmp0.toast.entities.interfaces.wobblingBehavior.DoWobble;
	import net.jmp0.toast.entities.interfaces.wobblingBehavior.WobblingBehavior;
	import punk.Acrobat;
	
	/**
	 * ...
	 * @author Thodd
	 */
	public class GameElement extends Acrobat
	{
		//Creating a GUID for this GameElement instance
		public var id:String = this + "@" + GUID.create();
		
		//Wobbling Strategy
		protected var wobblingBehavior:WobblingBehavior;
		
		//Standard moving Variables
		public var xSpeed:int = 0;
		public var ySpeed:int = 0;
		
		//This variables saves the reference on the last element the player collided with
		public var lastCollision:GameElement;
		
		//Player states
		public var isMoving:Boolean = false;
		public var isBurning:Boolean = false;
		
		//Moving variables
		public var movingSpeed:int = 7;
		public var oldXspeed:int = 0;
		public var oldYspeed:int = 0;
		
		/**
		 * GamneElement Constructor
		 * @param	x	the X coordinate for this GameElement instance
		 * @param	y	the Y coordinate for this GameElement instance
		 */
		public function GameElement(x:int, y:int)
		{
			this.x = x;
			this.y = y;
			
			//Standard WobblingBehavior initiation
			//adding it to the World as a seperate Entity, though invisible
			//this is important, because the behavior needs to be updated every step
			this.wobblingBehavior = new DoWobble(this);
			FP.world.add(wobblingBehavior as Acrobat);
			
			//Constructing an unique id for this instance
			trace("object instantiated, the id is: " + this.getID());
			
		}
		
		/**
		 * Returns the wobbling behavior instance
		 * @return an object implementing the WobblingBehavior interface
		 */
		public function getWobblingBehavior():WobblingBehavior {
			return this.wobblingBehavior;
		}
		
		
		//Draw the sprite, translated by drawX and drawY.
		//drawX and drawY are calculated by the wobbling Behavior
		override public function render():void {
			this.updateImage(sprite.number); //needs to be called in order to animate the sprite with the correct delay
			drawSprite(this.sprite, this.image, x + this.wobblingBehavior.getDrawX(), y + this.wobblingBehavior.getDrawY(), false, false);
		}
		
		/**
		 * Used to randomize the animation of this instance
		 * I don't want all objects to be animated synchron
		 */
		protected function randomizeAnimation():void {
			this.image = Math.round(sprite.number * Math.random());
		}
		
		/**
		 * returns the generated unique ID of each GameElement
		 * @return a String representing the GUID of this GameElement
		 */
		public function getID():String {
			return id+" at p("+x+","+y+")";
		}
		
		/**
		 * Destroys this instance and cleans up the wobblingBehavior instance if necessary
		 */
		public function destroy():void {
			if(wobblingBehavior != null){
				wobblingBehavior.destroy();
			}
			FP.world.remove(this);
		}
		
		/**
		 * This function is implemented in the subclasses of GameElement
		 * Search there for informations about what this method does
		 * @param	colPartner	the GameElement instance which collided with this GameElement
		 */
		public function performCollision(colPartner:GameElement):void {
			//implemented in subclasses
		}
		
	}

}